#!/bin/bash
 
# jdk install
# 请将下载的jdk-xxx-linux-xxx.tar.gz包与此脚本放置到同一目录
# 授予此脚本可执行权限(chmod +x install_jdk.sh)
# 在终端执行此脚本开始安装(./文件名)
# 注意：不可有多个版本的jdk包！
#      为了使配置的环境变量生效，安装完成后你应该重新登陆。
 
jvmpath=/usr/local/java
# 不存在
if [ ! -d "$jvmpath" ]; then
    echo "正在创建$jvmpath目录"
    sudo mkdir $jvmpath
    echo "目录$jvmpath创建成功"
fi
 
jdkfile=$(ls | grep jdk-*-linux-*.gz)
jdkdirname="jdk1.8.0_201"
 
if [ ! -f "$jdkfile" ]; then
   echo "正在下载jdk请稍等..."
   wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz"
fi
 
jdkfile=$(ls | grep jdk-*-linux-*.gz)
 
if [ -f "$jdkfile" ]; then
 
    sudo tar -zxvf $jdkfile -C /usr/local/java/
 
    echo "安装JDK成功"
 
    echo "配置环境变量"
 
    mv ~/.bashrc ~/.bashrc.backup.java
    cat ~/.bashrc.backup.java >> ~/.bashrc
    echo "PATH=\"$PATH:$jvmpath/$jdkdirname/bin\"" >> ~/.bashrc
    echo "JAVA_HOME=$jvmpath/$jdkdirname" >> ~/.bashrc
    echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> ~/.bashrc
    source ~/.bashrc
    echo "配置环境成功"
 
    echo "测试是否安装成功"
    java -version
    echo "安装成功"
 
fi